package com.bookmarket.controller;

import com.bookmarket.entity.Address;
import com.bookmarket.entity.Custom;
import com.bookmarket.service.LLAddressService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AdderssController
 *
 * @author 李亮
 * @date 2018/1/2
 */
@Controller
@RequestMapping("/address")
public class LLAddressController {
    @Resource
    private LLAddressService addressService;


    @RequestMapping("/getxinxi")
    public ModelAndView getxinxi(HttpSession session){
        ModelAndView mv = new ModelAndView();
        Custom custom=(Custom) session.getAttribute("NowCustom");
        mv.setViewName("stage/LLgerenziliao");
       return mv;
    }


    @RequestMapping("/getUser")
    public ModelAndView getUser(HttpSession session){
        ModelAndView mv = new ModelAndView();
        Custom custom=(Custom) session.getAttribute("NowCustom");
        Map map = new HashMap();
        map.put("user",custom);
        mv.setView(new MappingJackson2JsonView());
        mv.addObject(map);
        return mv;
    }


    @RequestMapping("/getaddress")
    public ModelAndView getall(HttpSession session) {
        Custom custom=(Custom) session.getAttribute("NowCustom");
        ModelAndView mv = new ModelAndView();
        List<Address> list=addressService.getAllList(custom.getId());
        mv.setView(new MappingJackson2JsonView());
        mv.addObject(list);
        return mv;
    }

    @RequestMapping("/updatepass")
    public ModelAndView updatepass(String password, String pass,HttpSession session) {
        ModelAndView mv = new ModelAndView();
     Custom custom=(Custom) session.getAttribute("NowCustom");
        System.out.println(custom.getPass());
     if(custom.getPass().equals(password)){
   addressService.updatepass(pass,custom.getId());
         mv.addObject("cheng",1);
     }else{  mv.addObject("cheng",2);
     }
        mv.setView(new MappingJackson2JsonView());

        return mv;
    }
    @RequestMapping("/dell")
    public ModelAndView dell(int id) {
        addressService.dell(id);
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("/add")
    public ModelAndView add(String name,String dizhi,String phone,HttpSession session) {
        Custom user=(Custom) session.getAttribute("NowCustom");
        ModelAndView mv = new ModelAndView();
     Address address=new Address(user,dizhi,name,phone);
       addressService.add(address);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    @RequestMapping("/update")
    public ModelAndView update(int id,String name,String dizhi,String phone) {
        ModelAndView mv = new ModelAndView();
        Address address=new Address(id,dizhi,name,phone);
        addressService.update(address);
        mv.setView(new MappingJackson2JsonView());
        mv.addObject(address);
        return mv;
    }






    public LLAddressService getAddressService() {
        return addressService;
    }

    public void setAddressService(LLAddressService addressService) {
        this.addressService = addressService;
    }
}